import useReportTitle from './useReportTitle'
import useLoadList, {
  type ReportList,
} from './useLoadList'
import useReportCell from './useReportCell'
import useArrowKey from './useArrowKey'
import useContextmenu from './useContextmenu'
import useReportRow from './useReportRow'
import useReportSave, {
  type StatementStore,
} from './useReportSave'
import useReportExport from './useReportExport'
import useReportInit from './useReportInit'

import type { LoadListParams, StatementStatus } from './types'
import { useStatementStore } from '@/store'

export { StatementStore }

export default function useAll(
  request: (params: LoadListParams) => Promise<any>,
  option?: {
    transfer?: (
      data: any,
      config: StatementStore
    ) => ReportList[]
    rowCallback?: (
      action: 'del' | 'append' | 'prepend'
    ) => any
    createAEmptyRow?: (store: StatementStore) => ReportList
    getCalc?: (
      row: number,
      prop: string,
      value?: string | number | undefined
    ) => number | string | undefined
    getReportData?: (store: StatementStore) => any
    assignExtraData?: (params: any, option: {
      data: any[]
      state: StatementStatus
      illustrate: string
    }) => void
    lengthPerRow?: number
  },
) {
  const statementStore = useStatementStore()
  const { statementColumns: columns }
    = storeToRefs(statementStore)

  const { title } = useReportTitle()
  const { lists, informant, loadList, loadPrevList }
    = useLoadList(request, option?.transfer)
  const { inputIndex, initInputIndex, clickCallback }
    = useReportCell()
  const { left, top, rowIndex, onContextmenu }
    = useContextmenu()
  const { delRow, appendRow, prependRow } = useReportRow(
    lists,
    rowIndex,
    {
      callback: option?.rowCallback,
      createAEmptyRow: option?.createAEmptyRow,
    },
  )
  const { report, saveStage } = useReportSave(
    lists,
    informant,
    {
      getCalc: option?.getCalc,
      getReportData: option?.getReportData,
      assignExtraData: option?.assignExtraData,
    },
  )
  const { exportFile } = useReportExport(lists, informant)

  useArrowKey(inputIndex, option?.lengthPerRow)
  useReportInit(loadList, initInputIndex)

  return {
    columns,
    title,
    lists,
    informant,
    left,
    top,
    onContextmenu,
    clickCallback,
    loadPrevList,
    delRow,
    appendRow,
    prependRow,
    report,
    saveStage,
    exportFile,
  }
}
